package com.lbd.finance.controller;

import com.lbd.commons.Constants;
import com.lbd.finance.bean.UserTradeDetail;
import com.lbd.finance.bean.vo.ExportUserTradeDetailVo;
import com.lbd.finance.bean.vo.UserTradeDetailVo;
import com.lbd.finance.service.UserTradeDetailService;
import com.lbd.base.controller.BaseController;
import com.lbd.commons.Result;
import com.lbd.commons.ResultState;
import com.lbd.system.service.DictionaryService;
import com.lbd.util.ExcelExportUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;


/**
 * 用户交易记录
 */
@RestController
@RequestMapping("/userTradeDetail")
public class UserTradeDetailController extends BaseController {
    private final Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private UserTradeDetailService usertradedetailService;

    @Autowired
    DictionaryService dictionaryService;

    /**
     * 按条件分页查询usertradedetail
     *
     * @param page            第几页
     * @param rows            一页多少条
     * @param usertradedetail
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result list(int page, int rows, UserTradeDetailVo usertradedetail) {
        Page<UserTradeDetail> tradeDetailPage = usertradedetailService.findByPageConditionJpql(page, rows, usertradedetail);
        return new Result(ResultState.SUCCESS, "查询成功", tradeDetailPage);
    }

    /**
     * 保存usertradedetail信息
     *
     * @param usertradedetail
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result save(UserTradeDetail usertradedetail) {
        usertradedetailService.save(usertradedetail);
        return new Result(ResultState.SUCCESS, "添加成功", "");
    }

    /**
     * 根据id查询回显要编辑的UserTradeDetail
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public Result toEdit(String id) {
        return new Result(ResultState.SUCCESS, "加载成功", usertradedetailService.findById(id));
    }

    /**
     * 修改usertradedetail信息
     *
     * @param usertradedetail
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Result update(UserTradeDetail usertradedetail) {
        usertradedetailService.update(usertradedetail);
        return new Result(ResultState.SUCCESS, "更新成功");
    }

    /**
     * 导出用户支出信息
     *
     * @param request         request
     * @param response        response
     * @param usertradedetail 查询条件
     */
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public void export(HttpServletRequest request, HttpServletResponse response, UserTradeDetailVo usertradedetail) {

        List<UserTradeDetail> userTradeDetailList = usertradedetailService.findByConditionJpql(usertradedetail);
        //处理数据字典信息
        String keys = Constants.DIC_USERTRADE_TYPE;
        Map<String, Map<String, String>> dictionaryMap = dictionaryService.getDictionary(keys);
        List<ExportUserTradeDetailVo> userTradeDetailVoList = new ArrayList<>();
        ExportUserTradeDetailVo exportUserTradeDetailVo;
        for (UserTradeDetail tradeDetail : userTradeDetailList) {
            exportUserTradeDetailVo = new ExportUserTradeDetailVo();
            try {
                BeanUtils.copyProperties(exportUserTradeDetailVo, tradeDetail);
            } catch (IllegalAccessException | InvocationTargetException e) {
                log.info("导出用户支出信息执行copy时出现异常：");
                log.info(e);
            }
            exportUserTradeDetailVo.setTelephone(tradeDetail.getUser().getTelephone());
            if (null != tradeDetail.getTrdDirec()) {
                exportUserTradeDetailVo.setTrdDirecName(tradeDetail.getTrdDirec() == Constants.TRD_DIRECT_1 ? "收入" : "支出");
            }
            exportUserTradeDetailVo.setTrdTypeName(dictionaryMap.get(Constants.DIC_USERTRADE_TYPE).get(tradeDetail.getTrdType().toString()));
            exportUserTradeDetailVo.setTrdTimeStr(DateFormatUtils.format(tradeDetail.getTrdTime(), "yyyy-MM-dd HH:mm:ss"));
            userTradeDetailVoList.add(exportUserTradeDetailVo);
        }

        ExcelExportUtils.exportExcel(request, response, DateFormatUtils.format(new Date(), "yyyyMMddHHmmss"), ExportUserTradeDetailVo.class, userTradeDetailVoList);

    }


}
